package com.yunkaweilai.android.activity.operation.score;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.member.IntegralChangeActivity;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.interfaces.MyDialogInterface;
import com.yunkaweilai.android.interfaces.ScoreClearInterface;
import com.yunkaweilai.android.model.member.MemberHeadModel;
import com.yunkaweilai.android.model.member.MemberInfoModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.dialog.MyDialog;
import com.yunkaweilai.android.view.dialog.ScoreClearDialog;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 积分调整
 */

public class ScoreChangeActivity2 extends BaseActivity implements RadioGroup.OnCheckedChangeListener, ScoreClearInterface, MyDialogInterface {
    @BindView(R.id.id_img_head)
    ImageView idImgHead;
    @BindView(R.id.id_tv_card_infor)
    TextView idTvCardInfor;
    @BindView(R.id.id_tv_name)
    TextView idTvName;
    @BindView(R.id.score_change2_et_score)
    EditText mEditTextScore;
    @BindView(R.id.score_change2_et_beizhu)
    EditText mEditTextBeizhu;
    @BindView(R.id.score_change2_btn_right)
    Button mButtonRight;
    @BindView(R.id.member_score)
    TextView mTextViewScore;
    @BindView(R.id.score_change2_radio_group)
    RadioGroup mRadioGroup;
    @BindView(R.id.radio_inc)
    RadioButton mRadioButtonInc;
    @BindView(R.id.radio_dec)
    RadioButton mRadioButtonDec;
    @BindView(R.id.score_change2_clear)
    TextView mButtonClear;
    @BindView(R.id.score_change2_log)
    TextView mButtonLog;
    @BindView(R.id.id_tv_change)
    TextView mTextViewChange;
    private static final String SCORE_MEMBER_ID = "SCORE_MEMBER_ID";
    private MemberHeadModel model;
    private String member_id;
    private String points_type = "";//积分处理类型
    private static final String INC_SCORE = "setInc";//增加积分传的参数
    private static final String DEC_SCORE = "setDec";//减少积分传的参数
    private static final String SET_REST_SCORE = "setRest";//积分清零传的参数

    //用传入member_id的方式打开这个activity
    public static void actionStart(Context context, String member_id) {
        Intent intent = new Intent(context, ScoreChangeActivity2.class);
        intent.putExtra(SCORE_MEMBER_ID, member_id);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score_change2);
        ButterKnife.bind(this);
        initview();

    }

    // 初始化视图
    private void initview() {
        //设置初始值,为增加积分
        mRadioGroup.setOnCheckedChangeListener(this);
        mRadioButtonInc.setChecked(true);

        //设置标题
        member_id = getIntent().getStringExtra(SCORE_MEMBER_ID);
        new TitleBuilder(mAc).setTitleText("积分调整").setLeftImage(R.mipmap.ic_go_back).setLeftOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //获取会员信息
        getMemberHead();

        //设置button点击事件
        mButtonRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setRight();
            }
        });
        mButtonClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Clear();
            }
        });
        mButtonLog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IntegralChangeActivity.actionStart(mAc, model);
            }
        });
    }

    // 按钮点击检查，无误再访问接口
    private void setRight() {
        if (mEditTextScore.getText().toString().isEmpty()) {
            showToast("积分不能为空");
            return;
        }
        if (mEditTextBeizhu.getText().toString().isEmpty()) {
            showToast("备注不能为空");
            return;
        }
        MyDialog myDialog = new MyDialog(mAc, ScoreChangeActivity2.this, "提示", "是否确定调整积分？");
        myDialog.show();

    }

    //积分清零
    private void Clear() {
        ScoreClearDialog myDialog = new ScoreClearDialog(mAc, "提示", "积分清零，清零后数据无法恢复!", ScoreChangeActivity2.this);
        myDialog.show();
    }

    //获取头部会员信息
    private void getMemberHead() {
        HttpRequest.postUrl(Api.MEMBER_INFO_BASIC)
                .addParams("member_id", member_id)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean codeInfoRs = Utils.checkCode(mAc, json);
                        if (codeInfoRs) {
                            Gson gson = new Gson();
                            MemberInfoModel memberInfoModel = gson.fromJson(json, MemberInfoModel.class);
                            model = memberInfoModel.getData().getList();
                            updateHead(model);
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                    }
                });
    }

    //设置头部会员信息
    private void updateHead(MemberHeadModel headModel) {
        mTextViewScore.setText(headModel.getMember_points());
        Utils.MemberHeadImg(mAc, model.getMember_avatar(), model.getMember_sex(), idImgHead);
        idTvName.setText(headModel.getMember_name() + "(" + headModel.getLevel_name() + ": " + headModel.getCard_no() + ")");
        idTvCardInfor.setText("折扣：" + headModel.getDiscount() / 10 + "折   积分：" + headModel.getMember_points() + "   余额：" + headModel.getMember_price());
    }

    //改变单选框时，改变要传的points_type
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int id) {
        switch (id) {
            case R.id.radio_inc:
                mTextViewChange.setText("调整＋");
                points_type = INC_SCORE;
                break;
            case R.id.radio_dec:
                mTextViewChange.setText("调整－");
                points_type = DEC_SCORE;
                break;
        }
    }


    //dialog的确定事件
    @Override
    public void clear(String text) {
        HttpRequest.postUrl(Api.APP_SCORE_CHANGE)
                .addParams("member_id", member_id)
                .addParams("points_type", SET_REST_SCORE)
                .addParams("remark", text)//备注
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode) {
                            showToast("操作成功");
                            getMemberHead();
                        } else {
                            showToast("服务器出错");
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        showToast(e.getMessage());
                    }
                });
    }

    @Override
    public void RightClick() {
        HttpRequest.postUrl(Api.APP_SCORE_CHANGE)
                .addParams("member_id", member_id)
                .addParams("points_type", points_type)
                .addParams("change_points", mEditTextScore.getText().toString())
                .addParams("remark", mEditTextBeizhu.getText().toString())
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode) {
                            showToast("操作成功");
                            mEditTextScore.setText("");
                            mEditTextBeizhu.setText("");
                            getMemberHead();
                        } else {
                            showToast("服务器出错");
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        showToast(e.getMessage());
                    }
                });
    }

    @Override
    public void CancelClick() {

    }
}
